<template>
  <div class="cateDialog">
    <el-dialog :title="title" :visible.sync="dialogStatus" :before-close="handleClose">
      <el-form label-width="80px">
        <el-form-item label="用户名">
          <el-input v-model="category.username"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" v-model="category.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="save">保存</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
export default {
  props: ["dialogVisible", "title", "dialogId", "parents"],
  data() {
    return {
      category: {
        username: "",
        password: ""
      }
    };
  },
  methods: {
    handleClose() {
      this.category.username = "";
      this.category.password = "";
      this.$emit("close", false);
    },
    async save() { //设置分类
      if (this.dialogId) { //修改
        await this.$http.put("rest/admin_accounts/" + this.dialogId, this.category);
      } else { //新增
        await this.$http.post("rest/admin_accounts", this.category);
      }
      this.handleClose();
      this.$emit("getCategory");
    },
    async getCateById(id) { //通过id获取分类信息
      const res = await this.$http.get("rest/admin_accounts/" + id);
      this.category = res.data;
      this.$forceUpdate();
    },
  },
  watch: {
    dialogStatus(val) {
      val && this.dialogId && this.getCateById(this.dialogId);
    }
  },
  computed: {
    dialogStatus() {
      return this.dialogVisible;
    }
  }
};
</script>
<style lang='scss' scoped>
</style>